<?php
namespace app\__MODULE__\controller;
use app\__MODULE__\model\ProductRule as ProductRuleModel;

class ProductRule extends Base {

	/**
	 * [index 首页列表]
	 * @return [type] [description]
	 */
	public  function index(){
		$search  = request()->param();
        $where = [];
        if(isset($search['title'])&&$search['title'] !=''){
            $where[] = ['rule_name','like',"%{$search['title']}%"];
        } 
		if(IS_POST) {
            $db   = ProductRuleModel::where($where)->order('id desc');
            $list = $db->limit(paginator())->select();
            if(count($list)){
                foreach($list as $k=>$item){
                    if ($item['rule_value']) {
	                    $item['specs'] = json_decode($item['rule_value'], true);
	                    $item['attr_name'] = array_column($item['specs'], 'value');
	                    $item['attr_value'] = array_column($item['specs'], 'detail');
	                }
                }
            }
  
            __MODULE___response(0, 'success', $list, $db->count());
        }

        return $this->render_table([
           "page"=>true,
            "toolbar" => [
                table_button('__MODULE__/ProductRule/add',"添加规格模板","frame",'',['850px','600px']),
            ],
            "toolbar_row" => [ 
                    table_button('__MODULE__/ProductRule/edit',"编辑","frame",'',['850px','600px']),
                    table_button('__MODULE__/ProductRule/del',"删除","confirm")
                    ],
            "cols" => [ 
                     
                    ["field"=>"id", "title"=>"编号","align"=>"center"],
                    ["field"=>"rule_name", "title"=>"规则名称","align"=>"center"],
                    ["field"=>"attr_name", "title"=>"商品规格","align"=>"center"],
                    ["field"=>"attr_value", "title"=>"商品属性","align"=>"center"],

            ],
            "where"=>[
                'title'  => ['type'=>'input', 'title'=>'关键字','placeholder'=>'题目名称搜索'],
            ]

            ],false);
	}

	/**
	 * 添加商品规则
	 */
	public function add($id=0){
		if(request()->isPost()) {
	        $data['rule_name'] = request()->post('rule_name','');
	        $data['rule_value'] = request()->post('rule_value/a',[]);
	        if ($data['rule_name'] == '') json_response(0,'请输入规则名称');
	        if (!$data['rule_value']) json_response(0,'缺少规则值');
	        $data['rule_value'] = json_encode($data['rule_value']);
	        ProductRuleModel::create($data);
	        json_response(1,'规则添加成功!');
         }else{
         	 $this->assign(compact('id'));
            return $this->fetch();
         }
	}

	/**
	 * 编辑商品规则
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public function edit(){
		$id = param_check('id');
		if(request()->isPost()) {
	        $data['rule_name'] = request()->post('rule_name','');
	        $data['rule_value'] = request()->post('rule_value/a',[]);
	        if ($data['rule_name'] == '') json_response(0,'请输入规则名称');
	        if (!$data['rule_value']) json_response(0,'缺少规则值');
	        $data['rule_value'] = json_encode($data['rule_value']);
	         $rule = ProductRuleModel::find($id);
	          if (!$rule) json_response(0,'数据不存在');
	            ProductRuleModel::update($data,['id'=>$id]);
	            json_response(1,'编辑成功!');
         }else{
         	 $this->assign(compact('id'));
            return $this->fetch();
         }
	}

	    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        return json_response(1,'success',ProductRuleModel::sysInfo($id));
    }


    /**
     * 删除
     * @return void
     * @author Sam
     * date 2021-08-16
     */
    public function del(){
        $id = param_check('id');
        if(!$rule = ProductRuleModel::get($id)) json_response(0,'规则不存在');
        if($rule->delete()) json_response(1,'删除成功');
        json_reponse(0,'删除失败');
    }
}
